<template>
	<view class="container">
		<uni-card is-full :is-shadow="false">
			<text class="uni-h6">使用Android跨进程通信</text>
		</uni-card>
		<view class="uni-title uni-common-mt">
			调试输出窗口
			<text>\n进行log和数据展示</text>
		</view>
		<view>
			<scroll-view scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<view class="text-box" scroll-y="true">
					<text>{{text}}</text>
				</view>
			</scroll-view>
		</view>
		<view class="uni-btn-v">
			<button type="primary" @click="bindWeighingServer">服务绑定（建立通信）</button>
			<button type="primary" @click="startWeighingReadIpc">发送指令开始读取</button>
			<button type="primary" @click="stopWeighingReadIpc">发送指令结束读取</button>
			<button type="primary" @click="unBindWeighingServer">服务绑定解除（关闭通信）</button>
		</view>
	</view>
</template>

<script>
	
	const zszyService = uni.requireNativePlugin('LittleNine-zszyService');

	export default {
		components: {},
		data() {
			return {
				text: '数据输出框\r\n',
				value: 0,
				customStyle: {
					backgroundColor: '#62ed0d',
					color: '#fff'
				}
			};
		},
		methods: {
			bindWeighingServer: function(e) {
				//绑定服务进行连接
				zszyService.bindWeighingServer(null, (data => {
					if (data.content) {
						this.text = this.text+"内容："+data.content
					}
					if (data.error) {
						this.text =this.text+ "错误："+data.error+"\r\n"
					}
					if (data.status) {
						//状态 200 连接成功 404连接断开 500 连接失败
						this.text = this.text+"状态："+data.status+"\r\n"
					}
				}));
			},
			startWeighingReadIpc: function(e) {
				//开始读取
				//key isVirtual value(boolean) true 使用虚拟数据 不传 则指在正式环境使用
				//key virtualData value(string) 'ww00030.0kg' 指定虚拟数据
				//key 
				//正式使用请zszyService.startWeighingReadIpc(null,null); 调用
				zszyService.startWeighingReadIpc({
					'isVirtual': true
				}, null);
			},
			stopWeighingReadIpc: function(e) {
				//停止读取
				//正式使用请zszyService.stopWeighingReadIpc(null,null); 调用
				zszyService.stopWeighingReadIpc({
					'isVirtual': true
				}, null);
			},
			unBindWeighingServer: function(e) {
				//解除绑定连接
				zszyService.unBindWeighingServer(null, null);
			},
		},
		mounted() {
			const timer = setInterval(() => {
				if (this.value >= 199) {
					clearInterval(timer)
					return
				}
				this.value++
			}, 100)
		}
	};
</script>

<style lang="scss">
	/* #ifdef MP-ALIPAY */
	.uni-badge {
		margin-left: 20rpx;
	}

	/* #endif */
	.example-body {
		flex-direction: row;
		justify-content: flex-start;
	}

	.uni-badge-left-margin {
		margin-left: 10px;
	}

	.uni-badge-absolute {
		margin-left: 40px;
	}

	.box {
		width: 40px;
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
		background-color: #DCDFE6;
		color: #fff;
		font-size: 12px;
	}

	.box-text {
		text-align: center;
		color: #fff;
		font-size: 12px;
	}

	.scroll-Y {
		height: 300rpx;
	}

	.text-box {
		margin-bottom: 40rpx;
		padding: 40rpx 0;
		display: flex;
		min-height: 300rpx;
		background-color: #FFFFFF;
		justify-content: left;
		align-items: center;
		text-align: left;
		font-size: 20rpx;
		color: #353535;
		line-height: 1.8;
	}
</style>